import csv
import traceback

import aiohttp
import asyncio


from aiohttp_socks import ProxyConnector

import logging
import pandas as pd

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

"""
 目前有两个问题：
 1，代码效率太低（优化时间）
"""


async def download(session, url):
    async with session.get(url) as response:
        return await response.status


async def test_proxy(ip, port):
    proxy = {
        "http": "http://" + ip + ":" + str(port),
        "https": "http://" + ip + ":" + str(port),
    }
    test_url = 'https://www.baidu.com'
    try:
        # 设置超时时间为 5 秒
        async with aiohttp.ClientSession(connector=ProxyConnector.from_url(proxy['https'])) as session:
            async with session.get(test_url, timeout=5) as response:
                if response.status == 200:
                    logging.info(f"代理 {proxy} 可用")
                    with open('proxy_data.csv', 'a', newline='', encoding='utf-8') as csvfile:
                        writer = csv.writer(csvfile)
                        # 写入表头，需要根据实际数据调整
                        writer.writerow(proxy[ip, port])
                    return True
                else:
                    logging.info(f"代理 {proxy} 不可用，状态码: {response.status}")
                    return False
    except asyncio.TimeoutError:
        logging.error(f"代理 {proxy} 请求超时（5秒）")
        return False
    except aiohttp.ClientError as e:
        error_msg = str(e) if str(e) else type(e).__name__
        logging.error(f"代理 {proxy} 客户端错误: {error_msg}")
        return False
    except Exception as e:
        logging.error(f"代理 {proxy} 未知错误: {type(e).__name__}")
        return False


# 示例代理，需要替换为实际代理
def get_proxy():
    data = pd.read_csv('proxy_data.csv')
    ip = data['IP']
    post = data['Port']
    return ip, post
async def main():
    ip, port = get_proxy()
    results = []
    batch_size = 100
    total = len(ip)
    all = 0
    for start in range(0, total, batch_size):
        end = min(start + batch_size, total)
        tasks = [test_proxy(ip[i], port[i]) for i in range(start, end)]
        print(tasks)
        batch_results = await asyncio.gather(*tasks)
        results.extend(batch_results)

        logging.info(f'第{start+batch_size}条ip测试完成')
        logging.info(f'测试结果为，{results}')
        for i in results:
            if i:
                all += 1
        logging.info(f'可用ip数量为{all}')




asyncio.run(main())  # 运行事件循环
